package com.hzw.saas.web.app.product.controller;


import com.hzw.saas.api.product.IOrderCacheService;
import com.hzw.saas.api.product.IPayService;
import com.hzw.saas.api.product.dto.OrderCacheDto;
import com.hzw.saas.api.product.dto.OrderStatusDto;
import com.hzw.saas.api.product.dto.PayUrlDto;
import com.hzw.saas.common.pay.enums.PayType;
import com.hzw.saas.common.security.pojo.dto.SaasUser;
import com.hzw.saas.common.security.utils.SecurityUtils;
import com.hzw.saas.web.app.product.param.BalanceOrderParam;
import com.hzw.saas.web.app.product.param.ItemOrderParam;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 支付表 前端控制器
 * </p>
 *
 * @author zzl
 * @since 2021-01-21
 */
@Controller
@RequestMapping("/pay")
@RequiredArgsConstructor
@Api(tags = "会员中心/支付中心")
@Slf4j
public class PaymentController {

    private final IPayService payService;
    private final IOrderCacheService orderCacheService;

    @GetMapping("/{payNo}/status")
    @ApiOperation(value = "查询订单状态", notes = "查询订单状态,一般用于购买页面内嵌二维码的情况，比如轮询查看用户是否已扫描二维码进行了支付")
    @ApiOperationSort(1)
    public ResponseEntity<OrderStatusDto> orderStatus(@PathVariable String payNo) {
        SaasUser user = SecurityUtils.getUser();
        OrderStatusDto orderStatusDto = payService.queryPayStatus(payNo, user.getUserId());
        return ResponseEntity.ok(orderStatusDto);

    }

    @PostMapping("/item/qrcode")
    @ApiOperation(value = "获取商品支付二维码", notes = "获取商品支付二维码，系统会生成对应订单缓存（目前仅支持支付宝，生成获取二维码的自动跳转表单）")
    @ApiOperationSort(2)
    public ResponseEntity<PayUrlDto> itemQRCode(@Validated @RequestBody ItemOrderParam param) {
        SaasUser user = SecurityUtils.getUser();
        OrderCacheDto orderCacheDto = orderCacheService.addItemOrder(user.getUserId(), param.getItemId(), param.getHpcAccountId(), param.getPayType());
        PayUrlDto QRCodeDto = payService.qrCodeGenerate(orderCacheDto, param.getQrCodeWith());
        return ResponseEntity.ok(QRCodeDto);
    }

    @PostMapping("/item/page")
    @ApiOperation(value = "获取商品支付收银台地址", notes = "获取商品支付收银台地址，系统会生成对应订单缓存（目前仅支持支付宝和银联，生成跳转到收银台自动跳转表单）")
    @ApiOperationSort(3)
    public ResponseEntity<PayUrlDto> itemPagePay(@Validated @RequestBody ItemOrderParam param) {
        SaasUser user = SecurityUtils.getUser();
        OrderCacheDto orderCacheDto = orderCacheService.addItemOrder(user.getUserId(), param.getItemId(), param.getHpcAccountId(), param.getPayType());
        PayUrlDto payUrlDto = payService.pagePayGenerate(orderCacheDto);
        return ResponseEntity.ok(payUrlDto);
    }

    @PostMapping("/item/balance")
    @ApiOperation(value = "获取商品支付余额购买参数", notes = "获取商品支付余额购买参数payNo，用于余额支付，系统会生成对应订单缓存")
    @ApiOperationSort(4)
    public ResponseEntity<PayUrlDto> itemBalancePay(@Validated @RequestBody ItemOrderParam param) {
        SaasUser user = SecurityUtils.getUser();
        OrderCacheDto orderCacheDto = orderCacheService.addItemOrder(user.getUserId(), param.getItemId(), param.getHpcAccountId(), PayType.BALANCE_PAY.value());
        return ResponseEntity.ok(new PayUrlDto().setPayNo(orderCacheDto.getPayment().getPayNo()));
    }


    @PostMapping("/balance/qrcode")
    @ApiOperation(value = "获取充值支付二维码", notes = "获取充值支付二维码，系统会生成对应订单缓存（目前仅支持支付宝，生成获取二维码的自动跳转表单）")
    @ApiOperationSort(5)
    public ResponseEntity<PayUrlDto> balanceQRCode(@Validated @RequestBody BalanceOrderParam param) {
        SaasUser user = SecurityUtils.getUser();
        OrderCacheDto orderCacheDto = orderCacheService.addBalanceOrder(user.getUserId(), param.getPayValue(), param.getPayType());
        PayUrlDto QRCodeDto = payService.qrCodeGenerate(orderCacheDto, param.getQrCodeWith());
        return ResponseEntity.ok(QRCodeDto);
    }

    @PostMapping("/balance/page")
    @ApiOperation(value = "获取充值支付收银台地址", notes = "获取充值支付收银台地址，系统会生成对应订单缓存（目前仅支持支付宝和银联，生成跳转到收银台自动跳转表单）")
    @ApiOperationSort(6)
    public ResponseEntity<PayUrlDto> balancePagePay(@Validated @RequestBody BalanceOrderParam param) {
        SaasUser user = SecurityUtils.getUser();
        OrderCacheDto orderCacheDto = orderCacheService.addBalanceOrder(user.getUserId(), param.getPayValue(), param.getPayType());
        PayUrlDto QRCodeDto = payService.pagePayGenerate(orderCacheDto);
        return ResponseEntity.ok(QRCodeDto);
    }

}
